package uilayer;
import ctrlayer.*;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.io.*;

/**
 * Textbased UI which allows the user to input information about persons
 * 
 * @author (Kasper) 
 * @version (08.12.2014)
 */
public class PersonUI
{
    private PersonCtr pCtr;
    private static PersonUI instance;

    /**
     * Constructor for objects of class PersonUI
     */
    public PersonUI()
    {
        pCtr = new PersonCtr();
    }

    /**
     * Singleton Method getInstance
     *
     * Creates an instance of PersonUI if one does not exist
     * 
     * @return instance of PersonUI
     */
    public static PersonUI getInstance()
    {
        if(instance == null)
        {
            instance = new PersonUI();
        }
        return instance;
    }

    /**
     * Method loadAnsatte
     *
     * Loads employees. This method is necessary to be called in the Main Menu.
     */
    public void loadAnsatte()
    {
        try
        {
            pCtr.loadAnsat();
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Der skete en fejl under indlæsning af ansatte");
        }
        catch(IOException e)
        {
            System.out.println("Der skete en fejl under indlæsning af ansatte");
        }
        pCtr.medarbejderNoCounterCheck();
    }

    public void loadKunder()
    {
        try
        {
            pCtr.loadKunde();
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Der skete en fejl under indlæsning af kunder");
        }
        catch(IOException e)
        {
            System.out.println("Der skete en fejl under indlæsning af kunder");
        }
        pCtr.kundeNoCounterCheck();
    }

    /**
     * Method personMenu
     *
     * This method checks for the userinput in the writePersonMenu method.
     * Each number results in different methods. If the user chooses 3 or greater the menu goes back to the previous menu.
     */
    public void personMenu()
    {
        boolean exit = false;
        pCtr.kundeNoCounterCheck();
        try
        {
            while(!exit)
            {
                int choice = writePersonMenu();
                switch(choice)
                {
                    //Åben kunde menu.
                    case 1:
                    kundeMenu();
                    break;
                    //Åben ansat menu.
                    case 2:
                    ansatMenu();
                    break;
                    //exit.
                    default:
                    exit = true;
                }
            }
        }
        catch (InputMismatchException e)
        {
            System.out.println("Du skal indtaste et tal!");
            personMenu();
        }
    }

    /**
     * Method kontrolLogin
     *
     * Calls the kontrolLogin method from the personcontroller. This takes a username and password as parameters.
     * If the method couldn't find a employee it returns false and prints a message.
     * 
     * @param brugernavn Username
     * @param password Password
     * @return False if the employee with the given parameters couldn't be found.
     */
    public boolean kontrolLogin(String brugernavn, String password)
    {   
        try
        {
            return pCtr.kontrolLogin(brugernavn, password);
        }
        catch(IndexOutOfBoundsException e)
        {
            System.out.println("Medarbejder findes ikke");
            return false;
        }
    }

    /**
     * Method loggetInd
     *
     * Calls the findLoggetInd method from the personcontroller. It takes a username as a parameter.
     * It returns the employeenumber of the person who is logged in at the moment.
     * 
     * @param brugernavn Username
     * @return Employeenumber
     */
    public String loggetInd(String brugernavn)
    {
        return pCtr.findLoggetInd(brugernavn);
    }

    /**
     * Method writePersonMenu
     *
     * The menu that allows the user to enter the customer- and employee menu. It is also possible to go back to the Main Menu.
     * 
     * @return An int based on one of the different choices.
     */
    private int writePersonMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Person menu");
        System.out.println(" (1) Kunder");
        System.out.println(" (2) Ansatte");
        System.out.println(" (3) Back");
        System.out.print("\n Vælg venligst en mulighed: ");
        int choice;
        while(!keyboard.hasNextInt())
        {
            System.out.println("Indtast venligst et tal!");
            keyboard.next();
        }
        choice = keyboard.nextInt();
        System.out.print("\n");
        return choice;
    }

    /**
     * Method kundeMenu
     *
     * Checks which return value is chosen in writeKundeMenu. Each choice results in different methods.
     * 
     */
    private void kundeMenu()
    {
        boolean exit = false;
        try
        {
            while(!exit)
            {
                int choice = writeKundeMenu();
                switch(choice)
                {
                    //opret kunde
                    case 1:
                    pCtr.createKunde(inputNavn(), inputAdresse(), inputPostnr(), inputBy(), inputTlf(), inputEmail());
                    break;
                    //opret fast kunde
                    case 2:
                    pCtr.createFastKunde(inputNavn(), inputAdresse(), inputPostnr(), inputBy(), inputTlf(), inputEmail(), inputKontoNr(), inputKundeGrp());
                    break;
                    //find kunde
                    case 3:
                    String tlf = inputTlf();
                    if(pCtr.findKunde(tlf) == null)
                    {
                        System.out.println("Kunden kunne desværre ikke findes. Prøv igen.");
                    }
                    else
                    {
                        System.out.println(pCtr.findKunde(tlf).toString());
                    }
                    break;
                    //Konverter kunde
                    case 4:
                    String tlf1 = inputTlf();
                    if(pCtr.findKunde(tlf1) == null)
                    {
                        System.out.println("Kunden kunne desværre ikke findes. Prøv igen.");
                    }
                    else
                    {
                        pCtr.konverterKunde(tlf1, inputKontoNr(), inputKundeGrp());
                    }
                    break;
                    //Rediger kunde
                    case 5:
                    String tlf2 = inputTlf();
                    if(pCtr.findKunde(tlf2) == null)
                    {
                        System.out.println("Kunden kunne desværre ikke findes. Prøv igen.");
                    }
                    else
                    {
                        pCtr.updateKunde(tlf2, inputNavn(), inputAdresse(), inputPostnr(), inputBy(), inputTlf(), inputEmail(), inputKontoNr(), inputKundeGrp());
                    }
                    break;
                    //Slet kunde
                    case 6:
                    pCtr.deleteKunde(inputTlf());
                    break;
                    //print kundeliste
                    case 7:
                    pCtr.printKundeInfo();
                    break;
                    //load kunde
                    case 8:
                    try
                    {
                        pCtr.loadKunde();
                    }
                    catch(ClassNotFoundException e)
                    {
                        System.out.println("Filen kunne ikke findes");
                    }
                    catch(IOException e)
                    {
                        System.out.println("Filen kunne ikke findes");
                    }
                    System.out.println("Filen er indlæst");
                    break;
                    //save kunde
                    case 9:
                    try
                    {
                        pCtr.saveKunde();
                    }
                    catch(IOException e)
                    {
                        System.out.println("Filen kunne ikke gemmes");
                    }
                    System.out.println("Filen er gemt");
                    break;
                    //exit
                    default:
                    exit = true;
                }
            }
        }
        catch (InputMismatchException e)
        {
            System.out.println("Du skal indtaste et tal!");
            kundeMenu();
        }
    }

    /**
     * Method writeKundeMenu
     *
     * The menu that allows the user to create, find, convert, update and delete customers.
     * The user can also print out a list of customers.
     * It is also possible to go back to the Person Menu.
     * 
     * @return An int based on one of the choices.
     */
    private int writeKundeMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Kunde menu");
        System.out.println(" (1) Opret kunde");
        System.out.println(" (2) Opret fast kunde");
        System.out.println(" (3) Find kunde");
        System.out.println(" (4) Konverter til fast kunde");
        System.out.println(" (5) Rediger kunde");
        System.out.println(" (6) Slet kunde");
        System.out.println(" (7) Print kundeliste");
        System.out.println(" (8) Indlæs informationer");
        System.out.println(" (9) Gem informationer");
        System.out.println(" (10) Back");
        System.out.print("\n Vælg venligst en mulighed: ");
        int choice;
        while(!keyboard.hasNextInt())
        {
            System.out.println("Indtast venligst et tal!");
            keyboard.next();
        }
        choice = keyboard.nextInt();
        System.out.println("\n");
        return choice;
    }

    /**
     * Method ansatMenu
     *
     * Checks which return value is chosen in writeAnsatMenu. Each choice results in different methods.
     */
    private void ansatMenu()
    {
        boolean exit = false;
        try
        {
            while(!exit)
            {
                int choice = writeAnsatMenu();
                switch(choice)
                {
                    //opret medarbejder
                    case 1:
                    pCtr.createMedarbejder(inputNavn(), inputAdresse(), inputPostnr(), inputBy(), inputTlf(), inputEmail());
                    break;
                    //opret leder
                    case 2:
                    pCtr.createLeder(inputNavn(), inputAdresse(), inputPostnr(), inputBy(), inputTlf(), inputEmail());
                    break;
                    //find ansat
                    case 3:
                    int nr = inputMedarbejderNr();
                    if(pCtr.findAnsat(nr)== null)
                    {
                        System.out.println("Den ansatte kunne desværre ikke findes. Prøv igen.");
                    }
                    else
                    {
                        System.out.println(pCtr.findAnsat(nr).toString());
                    }
                    break;
                    //Rediger ansat
                    case 4:
                    int nr1 = inputMedarbejderNr();
                    if(pCtr.findAnsat(nr1)== null)
                    {
                        System.out.println("Den ansatte kunne desværre ikke findes. Prøv igen.");
                    }
                    else
                    {
                        pCtr.updateAnsat(nr1, inputMedarbejderNr(), inputNavn(), inputAdresse(), inputPostnr(), inputBy(), inputTlf(), inputEmail(), inputPassword());
                    }
                    break;
                    //Slet ansat
                    case 5:
                    pCtr.deleteAnsat(inputMedarbejderNr());
                    break;
                    //print ansatliste
                    case 6:
                    pCtr.printAnsatInfo();
                    break;
                    //load ansat
                    case 7:
                    try
                    {
                        pCtr.loadAnsat();
                    }
                    catch(ClassNotFoundException e)
                    {
                        System.out.println("Filen kunne ikke findes");
                    }
                    catch(IOException e)
                    {
                        System.out.println("Filen kunne ikke findes");
                    }
                    System.out.println("Filen er indlæst");
                    break;
                    case 8:
                    try
                    {
                        pCtr.saveAnsat();
                    }
                    catch(IOException e)
                    {
                        System.out.println("Filen kunne ikke gemmes");
                    }
                    System.out.println("Filen er gemt");
                    break;
                    //exit
                    default:
                    exit = true;
                }
            }
        }
        catch (InputMismatchException e)
        {
            System.out.println("Du skal indtaste et tal!");
            kundeMenu();
        }
    }

    /**
     * Method writeAnsatMenu
     * 
     * The menu that allows the user to create, find, update and delete employees.
     * The user can also print out a list of employees.
     * It is also possible to go back to the Person Menu.
     * 
     * @return An int of based on one of the choices
     */
    private int writeAnsatMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Ansat menu");
        System.out.println(" (1) Opret medarbejder");
        System.out.println(" (2) Opret leder");
        System.out.println(" (3) Find ansat");
        System.out.println(" (4) Rediger ansat");
        System.out.println(" (5) Slet ansat");
        System.out.println(" (6) Print liste med ansatte");
        System.out.println(" (7) Indlæs informationer");
        System.out.println(" (8) Gem informationer");
        System.out.println(" (9) Back");
        System.out.print("\n Vælg venligst en mulighed: ");
        int choice;
        while(!keyboard.hasNextInt())
        {
            System.out.println("Indtast venligst et tal!");
            keyboard.next();
        }
        choice = keyboard.nextInt();
        System.out.println("\n");
        return choice;
    }

    public void save()
    {
        try
        {
            pCtr.saveKunde();
            pCtr.saveAnsat();
        }
        catch(IOException e)
        {
            System.out.println("Filen kunne ikke gemmes");
        }
        System.out.println("Filen er gemt");
    }

    /**
     * Method inputNavn
     *
     * @return A name
     */
    private String inputNavn()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Navn: ");
        String navn = keyboard.nextLine();
        return navn;
    }

    /**
     * Method inputAdresse
     *
     * @return An adress
     */
    private String inputAdresse()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Adresse: ");
        String adresse = keyboard.nextLine();
        return adresse;
    }

    /**
     * Method inputPostnr
     *
     * @return A Postal code
     */
    private String inputPostnr()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Postnummer: ");
        String postnr = keyboard.nextLine();
        return postnr;
    }

    /**
     * Method inputBy
     *
     * @return A city
     */
    private String inputBy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" By: ");
        String by = keyboard.nextLine();
        return by;
    }

    /**
     * Method inputTlf
     *
     * @return A phone number
     */
    private String inputTlf()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Telefonnummer: ");
        String tlf = keyboard.nextLine();
        return tlf;
    }

    /**
     * Method inputEmail
     *
     * @return An e-mail
     */
    private String inputEmail()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" E-mail: ");
        String email = keyboard.nextLine();
        return email;
    }

    /**
     * Method inputLogin
     *
     * @return A login name
     */
    private String inputLogin()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Brugernavn: ");
        String login = keyboard.nextLine();
        return login;
    }

    /**
     * Method inputPassword
     *
     * @return A password
     */
    private String inputPassword()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Password: ");
        String password = keyboard.nextLine();
        return password;
    }

    /**
     * Method inputMedarbejderNr
     *
     * @return An employee ID
     */
    private int inputMedarbejderNr()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Medarbejder Nr: ");
        int medarbejderNr = keyboard.nextInt();
        return medarbejderNr;
    }

    /**
     * Method inputKontoNr
     *
     * @return An accountnumber
     */
    private int inputKontoNr()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Kontonummer: ");
        int kontonr = keyboard.nextInt();
        return kontonr;
    }

    /**
     * Method inputKundeGrp
     *
     * @return A customergroup 
     */
    private int inputKundeGrp()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Kundegruppe: ");
        int kundeGrp = keyboard.nextInt();return kundeGrp;
    }
}